// neutron js api 消息接受

function min(a, b) {
    return a < b ? a : b;
}

window.addEventListener("message", receiveMessage, false);

function receiveMessage(event) {
    // 验证来源
    if (event.origin != location.origin) {
        return;
    }
    var data = event.data;
    if (data.from != "neutron_panel") {
        return;
    }

    if (data.type == "setTitle") { // 设置标题
        win_wininfo[data.winid].title = data.datas[0];
        document.getElementById("win_window-title-id-" + data.winid).innerText = data.datas[0];
        document.getElementById("win_header-button-text-id-" + data.winid).innerText = data.datas[0];
    } else if (data.type == "setMinSize") { // 最小大小
        win_wininfo[data.winid].minwidth = min(data.datas[0], window.innerWidth);
        win_wininfo[data.winid].minheight = min(data.datas[1], window.innerHeight - 32);
    } else if (data.type == "setWinSize") { // 设置大小
        document.getElementById("win_window-id-" + data.winid.toString()).style.transition = "none";
        setTimeout(() => {
            win_wininfo[data.winid].width = min(data.datas[0], window.innerWidth);
            win_wininfo[data.winid].height = min(data.datas[1], window.innerHeight - 32);
            document.getElementById("win_window-id-" + data.winid).style.width = data.datas[0] + "px";
            document.getElementById("win_window-id-" + data.winid).style.height = data.datas[1] + "px";
            setTimeout(() => {
                document.getElementById("win_window-id-" + data.winid.toString()).style.transition = null;
            }, 300)
        }, 0)
    } else if (data.type == "setPosition") { // 设置位置
        win_wininfo[data.winid].functions.moveto(data.datas[0], data.datas[1]);
    } else if (data.type == "createWin") { // 创建窗口
        var winid = win_create_window(data.datas[0], data.datas[1], data.datas[2], data.datas[3])
        event.source.postMessage({
            from: "neutron_panel_callback",
            callback_id: data.datas[4],
            datas: [winid]
        })
    } else if (data.type == "sendMessageToWin") { // 窗口间发送消息
        var winobj = document.getElementById("win_window-id-" + data.datas[0]);
        if (winobj == undefined) {
            return;
        }
        var iframeobj = winobj.getElementsByTagName("iframe")[0]
        if (iframeobj == undefined) {
            return;
        }
        iframeobj.contentWindow.postMessage(data.datas[1], "*");
    } else if (data.type == "saveData") { // 保存会话
        win_windata[data.winid] = data.datas[0];
    } else if (data.type == "loadData") { // 加载会话
        event.source.postMessage({
            from: "neutron_panel_callback",
            callback_id: data.datas[0],
            datas: [win_windata[data.winid]]
        })
    } else if (data.type == "getInfo") { // 获取窗口信息
        event.source.postMessage({
            from: "neutron_panel_callback",
            callback_id: data.datas[0],
            datas: [
                {
                    "x": win_wininfo[data.winid].x,
                    "y": win_wininfo[data.winid].y,
                    "w": win_wininfo[data.winid].width,
                    "h": win_wininfo[data.winid].height,
                }
            ]
        })
    } else if (data.type == "onClose") { // 设置关闭回调
        win_wininfo[data.winid].onclose = (function (winid) {
            event.source.postMessage({
                from: "neutron_panel_callback",
                callback_id: data.datas[0],
                datas: []
            })
            return 1
        })
    } else if (data.type == "close") { // 实际关闭
        win_wininfo[data.winid].functions.close(false)
    } else if (data.type == "context") { // 菜单
        create_menu(data.datas[1], data.datas[2] + document.getElementById("win_window-id-" + data.winid).getElementsByTagName("iframe")[0].getBoundingClientRect().left, data.datas[3] + document.getElementById("win_window-id-" + data.winid).getElementsByTagName("iframe")[0].getBoundingClientRect().top) // 调用上层函数
        menu_callback = function (callback_index) { // 设置回调
            event.source.postMessage({
                from: "neutron_panel_callback",
                callback_id: data.datas[0],
                datas: [callback_index]
            })
        }
    }
}


CHECK_DESKTOP_API_LOAD = 1